basket.require({
	url : 'http://cdn.bootcss.com/jquery/2.1.4/jquery.min.js'
}, {
	url : "http://cdn.bootcss.com/angular.js/1.4.3/angular.min.js"
}, {
	url : "http://cdn.bootcss.com/alertify.js/0.3.11/alertify.min.js"
}, {
	url : settings.getIp() + "/peiwan-server/static/js/persistence.js"
}, {
	url : settings.getIp() + "/peiwan-server/static/js/timer.js"
}).then(ready);

function ready() {
	persistence.setNamespace("peiwan_admin");
	var peiwanAdmin = angular.module('peiwan_admin', []);
	var userDetail = function(user) {
		var point = user.rateAmount / user.rateNumber;
		user.point = point;
		var gender = user.gender ? "女" : "男";
		user.gender = gender;
		var money = user.money ? user.money : 0.0;
		user.money = money;
		var lasttime = new Date(user.lastActiveTime)
				.format("yyyy-MM-dd hh:mm:ss");
		user.lasttime = lasttime;
	}
	peiwanAdmin.controller('PeiwanFormListCtrl', [ "$scope", function(s) {
		var offset = 0;
		var LIMIT = 5;
		if (!persistence.get("session")) {
			location.href = "admin_login.html";
			return;
		}
		var queryList = function() {
			ajax("/peiwanFormList", {
				session : persistence.get("session"),
				offset : offset,
				limit : LIMIT
			}, function(data) {
				var json = JSON.parse(data);
				if (json.status == 1) {
					location.href = "admin_login.html";
					return;
				}
				var peiwanForms = json.entity;
				for (var i = 0; i < peiwanForms.length; i++) {
					var user = peiwanForms[i].user;
					if (user) {
						userDetail(user);
					}
				}
				s.$apply(function() {
					s.peiwanForms = peiwanForms;
				});
			});
		}
		queryList();
		s.updateIsAudit = function(userId, isAudit) {
			ajax("/updateUser", {
				session : persistence.get("session"),
				userId : userId,
				isAudit : isAudit
			}, function(data) {
				var json = JSON.parse(data);
				if (json.status == 1) {
					location.href = "admin_login.html"
					return;
				}
				queryList();
				alertify.success("更新成功");
			});
		}
		s.deletePeiwanForm = function(peiwanFormId) {
			ajax("/deletePeiwanForm", {
				session : persistence.get("session"),
				peiwanFormId : peiwanFormId
			}, function(data) {
				var json = JSON.parse(data);
				if (json.status == 1) {
					location.href = "admin_login.html"
					return;
				}
				queryList();
				alertify.success("删除成功");
			});
		};
		s.prePage = function() {
			if (offset >= LIMIT) {
				offset -= LIMIT;
				var page = parseInt($("#page_input").val());
				$("#page_input").val(--page + "");
				queryList();
			}

		}
		s.toPage = function() {
			var page = parseInt($("#page_input").val());
			offset = (page - 1) * LIMIT;
			queryList();
		}
		s.nextPage = function() {
			offset += LIMIT;
			var page = parseInt($("#page_input").val());
			$("#page_input").val(++page + "");
			queryList();
		}
	} ]);
}